﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Xml.XPath;
using Yule8xCommon.Applib;
using Yule8xWeb.Typelib;

namespace Yule8xWeb.Models
{
    public class MenuModel
    {
        public static IList<MenuItemObject> GetMenus()
        {
            IList<MenuItemObject> list = new List<MenuItemObject>();
            MenuItemObject m1, m2;
            XElement xml = XElement.Parse(Reslib.Menu);
            IEnumerable<XElement> menus1, menus2;
            menus1 =xml.XPathSelectElements("/menu");
            foreach(XElement menu1 in menus1)
            {
                m1 = new MenuItemObject()
                {
                    name = XMLUtil.GetAttrValue(menu1, "name"),
                    text = XMLUtil.GetAttrValue(menu1, "text"),
                    ico = XMLUtil.GetAttrValue(menu1, "ico"),
                    url = XMLUtil.GetAttrValue(menu1, "url"),
                };
                menus2 = menu1.XPathSelectElements("menu");
                if (null != menus2)
                {
                    m1.children = new List<MenuItemObject>();
                    foreach(XElement menu2 in menus2)
                    {
                        m2 = new MenuItemObject()
                        {
                            name = XMLUtil.GetAttrValue(menu2, "name"),
                            text = XMLUtil.GetAttrValue(menu2, "text"),
                            ico = XMLUtil.GetAttrValue(menu2, "ico"),
                            url = XMLUtil.GetAttrValue(menu2, "url"),
                        };
                        m1.children.Add(m2);
                    }
                }
                list.Add(m1);                
            }
            return list;
        }
    }
}